package org.szrcb.temu.SalesManage.dao;

import org.apache.ibatis.annotations.*;
import org.szrcb.temu.SalesManage.pojo.MallInfo;

import java.util.List;

@Mapper
public interface MallInfoDao {
    @Select("SELECT count(1) FROM mall_info WHERE mall_id = #{mallId}")
    int countMallInfoById(@Param("mallId") String mallId);

    @Update("UPDATE mall_info SET mall_name = #{mallInfo.mallName},logo = #{mallInfo.logo},mall_status = #{mallInfo.mallStatus},is_semi_managed_mall = #{mallInfo.isSemiManagedMall} WHERE mall_id = #{mallInfo.mallId}")
    void updateMallInfo(@Param("mallInfo") MallInfo mallInfo);

    @Insert("INSERT INTO mall_info values (#{mallInfo.mallId},#{mallInfo.mallName},#{mallInfo.logo},#{mallInfo.mallStatus},#{mallInfo.isSemiManagedMall},#{mallInfo.vipState},null,null,null)")
    void insertMallInfo(@Param("mallInfo") MallInfo mallInfo);

    @Delete("DELETE from user_mall_rel WHERE user_id = #{userId}")
    void deleteUserMallRel(@Param("userId") String userId);

    @Insert("INSERT INTO user_mall_rel values (#{userId},#{mallId})")
    void insertUserMallRel(@Param("userId")String userId, @Param("mallId")String mallId);

    @Select("SELECT " +
            "mi.mall_id," +
            "mi.mall_name " +
            "FROM " +
            "user_mall_rel umr " +
            "LEFT JOIN mall_info mi ON umr.mall_id = mi.mall_id " +
            "WHERE " +
            "umr.user_id = #{userId} " +
            "AND mi.vip_state = 1")
    List<MallInfo> queryMallList(@Param("userId") String userId);

    @Select("SELECT\n" +
            "\tb.* \n" +
            "FROM\n" +
            "\tuser_mall_rel a\n" +
            "\tLEFT JOIN mall_info b ON a.mall_id = b.mall_id \n" +
            "WHERE\n" +
            "\ta.user_id = #{userId}")
    List<MallInfo> queryUserMallList(@Param("userId") String userId);


    @Select("<script>UPDATE mall_info SET vip_state =  1 WHERE mall_id in"
            + "<foreach collection='mallIdList' item='item' open='(' separator=',' close=')'> "
            + "#{item} "
            + "</foreach> " +
            "</script>")
    void updateVipState(@Param("mallIdList") List<String> mallIdList);

}
